package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.AnonymousAccess;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.Result;
import com.ruoyi.system.domain.Product;
import com.ruoyi.system.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/product")
public class ProductController  extends BaseController {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    @AnonymousAccess
    public Result<List<Product>> list(Product product) {
        if(Objects.isNull(product.getCategoryId())){
            throw new ServiceException("分类ID不能为空");
        }
        if(Objects.equals(product.getCategoryId(), 0L)){
            product.setCategoryId(null);
        }
        if(Objects.equals(product.getCategoryId(), -1L)){
            product.setCategoryId(null);
            product.setIsHot(1);
        }

        product.setStatus(1);
        List<Product> list = productMapper.selectProductList(product);
        return Result.OK(list);
    }

    /**
     * 查询商品
     */
    @GetMapping("/query")
    @AnonymousAccess
    public Result<Product> query(Product product) {
        if(Objects.isNull(product.getId())){
            throw new ServiceException("商品ID不能为空");
        }

        Product result = productMapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getStatus, 1).eq(Product::getId, product.getId()));
        return Result.OK(result);
    }

}
